class MyLayer{
    //iframe弹框显示页面
    //参数：layer:layer对象 title：弹框标题 contentUrl：iframe的路径 butObj：按钮的回调对象 size：窗口大小  end：销毁时的回调函数
    /*
    butObj格式(自带取消功能) ：{"保存"：function(){}}
    size格式："middle"/"big"/"small"
    end格式： function(){}
     */
    static async iframeLayer(layer, title, contentUrl, butObj,size="middle",endFun) {
        //按钮名数组
        let btns = ["取消"];
        //按钮事件对象
        let funs = {"btn1":function(index, layero){ layer.close(index)}};
        //用于按钮计数
        let count=2
        let area
        endFun=endFun===undefined?()=>{}:endFun
        //设置大小
        switch(size){
            case "middle":
                area=['700px', '500px']
                break;
            case "big":
                area=['800px', '600px']
                break;
            case "small":
                area=['500px', '400px']
                break;
        }
        for(let index in butObj){
            btns.push(index)
            funs["btn"+count]=butObj[index]
            index++
        }
            layer.open({
                type: 2 //此处以iframe举例
                , title: title
                , area: area
                , shade: 0
                , maxmin: true
                , content: contentUrl
                , btn: btns
                , ...funs
                , zIndex: layer.zIndex
                , success: function (layero,index) {
                    //获得新打开的window
                    let thisWindow=window.parent[window.parent.length-1]
                    //初始化相关值
                    thisWindow.iframeObj={}
                    thisWindow.iframeObj.preWindow=window
                    thisWindow.iframeObj.preIndex=layer.getFrameIndex(window.name)
                    thisWindow.iframeObj.index=index
                    layer.setTop(layero);
                }
                , end:endFun
            });
    }
    //确认删除
    static confimDeleteLayer(layer,fun) {
        layer.confirm('确认删除?', {icon: 0, title:'提示'}, fun);
    }
     //网络错误
     static netError(layer,fun) {
        layer.msg('网络错误', {icon: 2}); 
    }
    //表单校验 参数为form的id，符合条件返回true，不符合弹窗提示且返回flase
    static formcheck(formId){
        let $ = layui.$
            ,layer = layui.layer
            ,form = layui.form
            ,hint = layui.hint()
            ,device = layui.device()
            ,stop = null
            ,verify = form.config.verify //验证规则
            ,DANGER = 'layui-form-danger' //警示样式
            ,verifyElem = $("#"+formId).find('*[lay-verify]') //获取需要校验的元素

            //开始校验
            layui.each(verifyElem, function(_, item){
                var othis = $(this)
                    ,vers = othis.attr('lay-verify').split('|')
                    ,verType = othis.attr('lay-verType') //提示方式
                    ,value = othis.val();

                othis.removeClass(DANGER); //移除警示样式

                //遍历元素绑定的验证规则
                layui.each(vers, function(_, thisVer){
                    var isTrue //是否命中校验
                        ,errorText = '' //错误提示文本
                        ,isFn = typeof verify[thisVer] === 'function';

                    //匹配验证规则
                    if(verify[thisVer]){
                        var isTrue = isFn ? errorText = verify[thisVer](value, item) : !verify[thisVer][0].test(value);
                        errorText = errorText || verify[thisVer][1];

                        if(thisVer === 'required'){
                            errorText = othis.attr('lay-reqText') || errorText;
                        }
                        //如果是必填项或者非空命中校验，则阻止提交，弹出提示
                        if(isTrue){
                            //提示层风格
                            if(verType === 'tips'){
                                layer.tips(errorText, function(){
                                    if(typeof othis.attr('lay-ignore') !== 'string'){
                                        if(item.tagName.toLowerCase() === 'select' || /^checkbox|radio$/.test(item.type)){
                                            return othis.next();
                                        }
                                    }
                                    return othis;
                                }(), {tips: 1});
                            }
                            else if(verType === 'alert') {
                                layer.alert(errorText, {title: '提示', shadeClose: true});
                            }
                            else {

                                layer.msg(errorText, {icon: 5, shift: 6,time: 2000});
                            }
                            //非移动设备自动定位焦点
                            if(!device.android && !device.ios){
                                setTimeout(function(){
                                    item.focus();
                                }, 3000);
                            }
                            othis.addClass(DANGER);
                            return stop = true;
                        }
                    }
                });
                if(stop) return stop;
            });
        return stop?false:true;
    }
}
